package com.zyk.controller;

import com.zyk.entity.TblQuestion;
import com.zyk.service.TblQuestionService;


import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;


/**
 * 问题表(TblQuestion)表控制层
 *
 * @author makejava
 * @since 2021-11-23 15:05:17
 */
@Controller
@RequestMapping("tblQuestion")
public class TblQuestionController {
    /**
     * 服务对象
     */
    @Resource
    private TblQuestionService tblQuestionService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TblQuestion> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.tblQuestionService.queryById(id));
    }



    /**
     * 查询所有，跳转到问题管理页面
     */
    @RequestMapping("findAll")
    public ModelAndView  findAllQuestion(){
        ModelAndView modelAndView= new ModelAndView();
        List<TblQuestion> questionList = tblQuestionService.queryAll();
        for (TblQuestion tblQuestion : questionList) {
            System.out.println(tblQuestion.toString());
        }
        //设值
        modelAndView.addObject("questionList", questionList);
        modelAndView.setViewName("showQuestion");
        //设置跳转的页面
        return modelAndView;
    }

    /**
     * 新增数据
     *
     * @param tblQuestion 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<TblQuestion> add(TblQuestion tblQuestion) {
        return ResponseEntity.ok(this.tblQuestionService.insert(tblQuestion));
    }

    /**
     * 编辑数据
     *
     * @param tblQuestion 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<TblQuestion> edit(TblQuestion tblQuestion) {
        return ResponseEntity.ok(this.tblQuestionService.update(tblQuestion));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.tblQuestionService.deleteById(id));
    }

}

